/**
 * Created With IntelliJ IDEA
 * Description:牛客网：BM57 岛屿数量
 * <a href="https://www.nowcoder.com/practice/0c9664d1554e466aa107d899418e814e?tpId=295&tqId=1024684&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj">...</a>
 * User: DELL
 * Data: 2023-02-28
 * Time: 23:06
 */
public class Solution {
    /**
     * 法一：深度优先算法
     * 递归地将一整个岛屿的‘1’全部置为‘0’后递归结束
     * @param grid char字符型二维数组
     * @return int整型
     */
    private void dfs (char[][] grid, int i, int j) {
        grid[i][j] = '0';
        //分别判断相邻是否为‘1’
        if (i - 1 >= 0 && grid[i-1][j] == '1') {
            dfs(grid,i-1,j);
        }
        if (i + 1 < grid.length && grid[i+1][j] == '1') {
            dfs(grid,i+1,j);
        }
        if (j - 1 >= 0 && grid[i][j-1] == '1') {
            dfs(grid,i,j-1);
        }
        if (j + 1 < grid[0].length && grid[i][j+1] == '1') {
            dfs(grid,i,j+1);
        }
    }
    public int solve (char[][] grid) {
        //判空处理
        if (grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int num = 0;
        int row = grid.length;
        int col = grid[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid,i,j);
                    num++;
                }
            }
        }
        return num;
    }
    /**
     * 法二：广度优先算法
     * 因为广度优先搜索算法与深度优先搜索相比，就是不采用递归，而是采用将每一个‘1’周边
     * 的‘1’的坐标均压入队列中，并将其改为‘0’，之后再对队列中的元素进行相同操作。
     * @param grid char字符型二维数组
     * @return int整型
     */

}